<?php

class Wards extends MX_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->library('pagination');  
    }
    
    function index($row=0)
    {
        if(!($this->session->userdata('login'))) redirect('administrator/login');
        //Check Quyền
        //if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
        //redirect('administrator/index');

        if($this->input->post('txtname'))
        {
            //add Term
            $ward = new Ward();
            $ward->name = $this->input->post('txtname');
            $ward->code = $this->input->post('txtcode');
            $ward->district_id = $this->input->post('ddlDistrict');

            $ward->save();
        }

        $ward = new Ward();            
            
        include('paging.php');
        $config['base_url']= base_url()."administrator/wards/index/";
        $config['total_rows'] = $ward->count();
        $config['cur_page']= $row;
        $config['num_links'] = 3;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	

        $data['lstWard'] = $ward->limit($config['per_page'], $row)->get();
        
        $province = new Province();
        $data['lstProvince'] = $province->order_by("name", 'ASC')->get(); 
        $data['view'] = 'wards_index';
        $this->load->view('back_end/template_noright',$data);
    }
        
    function edit($id,$row=0)
    {
        if(!($this->session->userdata('login'))) redirect('administrator/login');
        //Check Quyền
        //if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
        //redirect('administrator/index');

        if($this->input->post('txtname'))
        {
            $id = $this->input->post('ward_id');

            $ward = new Ward();
            $ward->where('id',$id)->get();
            $ward->name = $this->input->post('txtname');
            $ward->code = $this->input->post('txtcode');
            $ward->province_id = $this->input->post('ddlProvince');

            $ward->save();
            redirect('administrator/wards');
        }

        $ward = new Ward();            

        include('paging.php');
        $config['base_url']= base_url()."administrator/wards/edit/".$id.'/';
        $config['total_rows'] = $ward->count();
        $config['cur_page']= $row;
        $config['num_links'] = 3;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	

        $data['lstWard'] = $ward->limit($config['per_page'], $row)->get();

        $province = new Province();
        $data['lstProvince'] = $province->order_by("name", 'ASC')->get(); 
        $ward = new Ward();
        $data['ward'] = $ward->where('id',$id)->get();
        $data['view'] = 'wards_edit';
        $this->load->view('back_end/template_noright',$data);
    }

    function delete()
    {
        $id = $this->input->post('param');
        $ward = new Ward();
        $ward->where('id',$id)->get();
        $ward->delete();
    }
}
?>
